<?php

class Filter_Password implements Zend_Filter_Interface
{
    /**
     * Hash profile password.
     * Returns the result of filtering $value
     *
     * @param  mixed $value
     * @throws Zend_Filter_Exception If filtering $value is impossible
     * @return mixed
     */
    public function filter($value)
    {
    	if ($value == '') return $value;
    	$value = base64_encode($value);
    	$value = sha1($value);
    	$value = md5($value);
    	return $value;
    }
    
    /**
     * Шифрование пароля методом sha1 с salt примесями и blowfish кодированием
     * @param unknown_type $passwd
     */
    public function filterBySha1($passwd) {
    	
    	$cryptOptions = Config::getInstance()->user_password_crypt;
    	$salt = $cryptOptions->salt;
    	
		$passLen = strlen($passwd);
		$saltLen = strlen($salt);
		if ($passLen < $saltLen) {
    		$pos = round($saltLen/$passLen)-1;
			for ($i = 0; $i < $passLen; ++$i) {
				$salt[$pos*$i] = $passwd[$i];
			}
		} else {
			$salt = $passwd;
		}
		
		$hash = sha1($salt);
		
		$chain = new Zend_Filter();
		$crypt = new Zend_Filter_Encrypt($cryptOptions->toArray());
		$chain->addFilter($crypt);

		$filtered = base64_encode($chain->filter($hash));
		return $filtered;
    }
}